# coding:utf-8

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow


class Calc(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.x = None
        self.y = None
        self.result = None
        self.operator = None
        self.equal = None

        uic.loadUi('calc.ui', self)
        btns = [
            self.pushButton0,
            self.pushButton1,
            self.pushButton2,
            self.pushButton3,
            self.pushButton4,
            self.pushButton5,
            self.pushButton6,
            self.pushButton7,
            self.pushButton8,
            self.pushButton9,
            self.pushButton_clear,
            self.pushButton_add,
            self.pushButton_sub,
            self.pushButton_div,
            self.pushButton_mul,
            self.pushButton_dot,
            self.pushButton_equal
        ]

        for btn in btns:
            btn.clicked.connect(self.click_on_button)
    def calculate(self):
        """
        如果x、y、operater不为空，计算结果,计算出结果后，清空y,保持x和结果相等。
        """

        if self.x and self.y and self.operator:
            if self.operator == '+':
                s = round(float(self.x) + float(self.y), 2)
                s = self.bi_jiao(s)
            elif self.operator == '-':
                s = round(float(self.x) - float(self.y), 2)
                s = self.bi_jiao(s)              

            elif self.operator == '*':
                print '大爷好'
                s = round(float(self.x) * float(self.y), 2)
                s = self.bi_jiao(s)              
            elif self.operator == '/':
                if float(self.x) == 0 and float(self.y) == 0 and self.operator == '/':
                    s = '结果未定义'
                else:
                    s = round(float(self.x) / float(self.y), 2)
                    s = self.bi_jiao(s)                

            self.x = s 
            self.y = None
            self.text_result.setText(s)

    def click_on_button(self):

        print "点击按钮前：", self.x, self.operator, self.y
        value = self.sender().text()

        # 点击数字的处理：
        # 1   点数字时，无运算符且有x，累加x
        # 2   点数字时，有运算符且有y，累加y
        # 3   点数字时，无运算符且无x，赋值给x
        # 4   点数字时，有运算符且无Y，赋值给y

        if value in "0123456789.":
            if self.operator is not None:  # 有运算符
                if self.y is not None:  # 有y
                    if self.y == '0':
                        if value == '.' and self.y not in '.':
                            print 1
                            self.y = self.y + value
                        else:
                            print self.y, value
                            self.y = value
                    else:
                        if '.' not in self.y or value in '0123456789':
                            self.y = self.y + value

                else:  # 无x
                    if value == '.':
                        self.y = '0' + value
                    else:
                        self.y = value
                a = '%s%s%s'%(self.x,self.operator,self.y)
                self.text_result.setText(a)


            else:  # 无运算符
                if self.x is not None:  # 有x
                    if self.x == '0':
                        if value == '.' and self.x not in '.':
                            print 1
                            self.x = self.x + value
                        else:
                            print self.x, value
                            self.x = value
                    else:
                        if '.' not in self.x or value in '0123456789':
                            self.x = self.x + value

                else:  # 无x
                    if value == '.':
                        self.x = '0' + value
                    else:
                        self.x = value
                self.text_result.setText(self.x)

        # 开始处理运算符
        # 1.点运算符时记录运算符
        # 2.点运算符时计算结果，然后记录运算符(x,y,oper都有值)
        # 2.点运算符时记录运算符并计算结果(x,y,oper都有值)
        # 3.点运算符时记录运算符，并将x置为0(x无值)

        elif value in "+-*/":

            if self.x is None:
                self.x = '0'
            if self.x and self.y and self.operator:
                self.calculate()
            self.operator = value
            a = '%s%s'%(self.x,self.operator)
            self.text_result.setText(a)


        # 处理点击等号的操作
        # 1.点等号时计算结果 (x,y,oper都有值）
        # 2.点等号时忽略等号 (x,y,oper不都有值)

        elif value == "=":
            if self.x  and self.operator :
                if self.y == None:
                    self.y = self.x
                    self.calculate()
                else:
                    self.calculate()
            elif self.x  and self.y and self.operator :
                self.calculate()
        else:
            print "您点击的是C"
            self.clear()
        print "点击按钮后：", self.x, self.operator, self.y

    def clear(self):
        self.x = None
        self.y = None
        self.result = None
        self.operator = None
        self.text_result.setText("0")

    def bi_jiao(self,x):
        if  int(x) == float(x):
            return str(int(x))
        else:
            return str(x)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    calc = Calc()
    calc.show()
    app.exec_()
